// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Create civils car in a location
// + Rework of PDB function FlashpointChernarus107 - thomsonb 
// -----------------------------------------------

private [
	"_roads",
	"_road",
	"_dir",
	"_pos",
	"_car",
	"_marker",
	"_position",
	"_max"
];

_position = _this select 0;

WC_fnc_PDB_CC = {
	private ["_bearing","_distance","_pos"];
	_pos      = _this select 0;
	_bearing  = _this select 1;
	_distance = _this select 2;
	[(_pos select 0) + (_distance * (sin _bearing)), (_pos select 1) + (_distance * (cos _bearing))];
};

_roads = _position nearRoads 400;
{
	if !((count (roadsConnectedTo _x) > 1) and (count (nearestObjects [_x, ["House"], 20]) > 0)) then {
		_roads = _roads - [_x];
	};
} foreach _roads;

if (count _roads == 0) exitWith {diag_log "WARCONTEXT: NO ROADS IN THIS VILLAGE TO BUILD CIVIL CAR"};

_max = round (random wcwithcivilcar);

for "_x" from 1 to _max do {
	_road = (_roads call BIS_fnc_selectRandom);
	_dir = getDir _road;
	_pos = [getPos _road, _dir + 90, ceil (random 3)] call WC_fnc_PDB_CC;
	_car = (wcvehicleslistC call BIS_fnc_selectRandom) createVehicle _pos;
	_car setPos [(position _car select 0), (position _car select 1), 1];
	_car setDir _dir;

	_marker = [format ["mrkcivilcar%1", wccivilcarindex], 0.5, position _car, "ColorBlack", "ICON", "FDIAGONAL", "dot", 0, "", false] call WC_fnc_createmarkerlocal;
	wcambiantmarker = wcambiantmarker + [_marker];
	wccivilcarindex = wccivilcarindex + 1;
	diag_log format ["WARCONTEXT: GENERATE %1 CIVIL CAR", typeOf _car];

	// Simulation mode
	if (wckindofgame == 2) then {
		_car setFuel (random 1);
		_car setDamage (random 1);
	};

	if (random 1> 0.95) then {
		_car setVectorUp [1, 0, 0];
		_car setDamage (random 1);
	};

	if (random 1 > 0.95) then {
		wcgarbage = [_car] spawn WC_fnc_nastyvehicleevent;
	};

	wcvehicles = wcvehicles + [_car];
};